﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameObjects
{
    public class StupidMonsterMove : IMoveBehavior
    {
        private Character _character;
        public Character Character
        {
            get { return _character; }
            set { _character = value; }
        }
        Vector2 _NewPosition;
        public Vector2 NewPosition
        {
            get { return _NewPosition; }
            set { _NewPosition = value; }
        }
        List<Character> targets;

        public StupidMonsterMove(Character character)
        {
            this._character = character;
            this.targets = new List<Character>();
        }

        public StupidMonsterMove(Character character, params Character[] targets)
        {
            this._character = character;
            this.targets = new List<Character>(targets);
        }

        public void AddTarget(Character target)
        {
            targets.Add(target);
        }

        public void RemoveTarget(Character target)
        {
            targets.Remove(target);
        }

        public void Move(GameTime gameTime)
        {
            if (targets.Count != 0)
            {
                // ищем ближайшего врага
                // находим расстояние до ближайшего врага
                Vector2 distance = (targets[0].Position - _character.Position);
                for (int i = 0; i < targets.Count; i++)
                {
                    Vector2 tmpDistance = (targets[i].Position - _character.Position);
                    if (tmpDistance.Length() < distance.Length())
                        distance = tmpDistance;
                }
                float delta = (float)(_character.Speed * gameTime.ElapsedGameTime.TotalSeconds);
                distance.X = distance.X >= delta ? 1 : (distance.X <= -delta ? -1 : 0);
                distance.Y = distance.Y >= delta ? 1 : (distance.Y <= -delta ? -1 : 0);
                _NewPosition = _character.Position + distance * delta;
            }
        }
    }
}
